var express = require('express');
var app = express.Router();
//      用户的数据模型，地址的数据模型，商品类型的数据模型，详细地点的数据模型，用户的数据模型，评论的数据模型
const { rui_ShopModel, rui_addrModel, rui_ShopTypeModel, rui_placeModel, rui_commentModel } = require('../mondules/rui/rui_module');

const { File } = require('../mondules/csy/csy_module');


// ===============================================================================================================
// 商品的API接口

// 商品添加
app.post('/shopAdd', (req, res) => {
  rui_ShopModel.create(req.body);
  res.send({
    code: 200,
    msg: '添加成功'
  })
});

// 商品列表
app.get('/shopList', async (req, res) => {
  const shopData = await rui_ShopModel.find()
  res.send({
    code: 200,
    shopData
  })
})

app.get('/detail', async (req, res) => {
  const { id } = req.query
  const detailId = id.split(':')
  const detailIdJoin = detailId.join('')

  const detail = await rui_ShopModel.findOne({ _id: detailIdJoin })
  res.send({
    code: 200,
    detail
  })
})

app.get('/shopDetail', async (req, res) => {
  const { id } = req.query

  const details = await rui_ShopModel.findOne({ _id: id })
  
  res.send({
    code: 200,
    details
  })
})

app.post('/static', async (req, res) => {
  const { id, num } = req.body
  const static = await rui_ShopModel.findOne({ _id: id })

  if (num == 1) {
    static.data = 1
  } else if (num == 2) {
    static.data = 2
  } else if (num == 3) {
    static.data = 3
  } else {
    static.data = 4
  }

  await static.save()

  res.send({
    code: 200,
    static
  })
})


// ==============================================================================================================
// 地址的API接口

// 地址添加
app.post('/addrAdd', (req, res) => {
  rui_addrModel.create(req.body.params)
  res.send({
    code: 200,
    msg: '添加成功'
  })
})


// 地址列表
app.get('/addrList', async (req, res) => {
  const addrData = await rui_addrModel.find().populate('title')
  res.send({
    code: 200,
    addrData
  })
})

app.get('/editDetail', async (req, res) => {
  const { id } = req.query
  const detail = await rui_addrModel.findOne({ _id: id })
  res.send({
    code: 200,
    detail
  })
})

app.post('/addrUpdate', async (req, res) => {
  const { id } = req.body.params
  await rui_addrModel.updateOne({ _id: id }, req.body.params)
  res.send({
    code: 200,
    msg: '修改成功'
  })
})

app.post('/detailStatic', async (req, res) => {
  const { id } = req.body.params

  await rui_addrModel.updateMany(
    { default: true }, // 假设 req.user.id 是当前用户的 ID
    { $set: { default: false } }
  );

  // 然后将指定地址设置为默认
  await rui_addrModel.findByIdAndUpdate(
    id,
    { default: true },
    { new: true }
  );

  res.send({
    code: 200,
  })
})

app.post('/addrDel', async (req, res) => {
  const { id } = req.body.params
  console.log(id);

  await rui_addrModel.deleteOne({ _id: id })
  res.send({
    code: 200,
    msg: '删除成功'
  })
})


// ==============================================================================================================
// 商品类型的API接口

//类型的添加
app.post('/shopTypeAdd', (req, res) => {
  rui_ShopTypeModel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功'
  })
})

// 类型的列表
app.get('/shopTypeList', async (req, res) => {
  const typeData = await rui_ShopTypeModel.find()
  res.send({
    code: 200,
    typeData
  })
})



// ==============================================================================================================
// 详细地址的API接口
// 详细地址的接口

app.post('/placeAdd', (req, res) => {
  rui_placeModel.create(req.body)
  res.send({
    code: 200,
    msg: '添加成功'
  })
})

app.get('/placeList', async (req, res) => {
  const placeData = await rui_placeModel.find()
  res.send({
    code: 200,
    placeData
  })
})



// ==============================================================================================================
// 用户的API接口

// 笔记广场的API接口
app.post('/noteList', async (req, res) => {
  const { id } = req.body.params
  const noteList = await File.findOne({ _id: id })

  if (noteList.flag == true) {
    noteList.flag = false
    noteList.like -= 1
  } else {
    noteList.flag = true
    noteList.like += 1
  }
  noteList.save()
  res.send({
    code: 200,
    noteList
  })
})

module.exports = app;